﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XYZShell.CSHelper.Data
{
    /// <summary>
    /// 指令参数
    /// </summary>
    public sealed class StatementParameterCollection : KeyedCollection<string, StatementParameter>
    {
        public StatementParameterCollection()
            : base(StringComparer.CurrentCultureIgnoreCase)
        {
        }

        /// <summary>
        /// 添加Input类型的参数
        /// </summary>
        /// <param name="name">参数名，建议有@符号，如果没有，框架会补偿</param>
        /// <param name="dbType">DbType</param>
        /// <param name="value">参数值</param>
        public void AddInParameter(string name, DbType dbType)
        {
            this.AddInParameter(name, dbType, null);
        }

        /// <summary>
        /// 添加Input类型的参数
        /// </summary>
        /// <param name="name">参数名，建议有@符号，如果没有，框架会补偿</param>
        /// <param name="dbType">DbType</param>
        /// <param name="value">参数值</param>
        public void AddInParameter(string name, DbType dbType, object value)
        {
            this.AddInParameter(name, dbType, value, false);
        }

        /// <summary>
        /// 添加Input类型的参数
        /// </summary>
        /// <param name="name">参数名，建议有@符号，如果没有，框架会补偿</param>
        /// <param name="dbType">DbType</param>
        /// <param name="value">参数值</param>
        /// <param name="sensitive">参数是否敏感，如果敏感，不记入Log系统</param>
        public void AddInParameter(string name, DbType dbType, object value, bool sensitive)
        {
            this.AddInParameter(name, dbType, value, sensitive, false);
        }

        /// <summary>
        /// 添加Input类型的参数
        /// </summary>
        /// <param name="name">参数名，建议有@符号，如果没有，框架会补偿</param>
        /// <param name="dbType">DbType</param>
        /// <param name="value">参数值</param>
        /// <param name="sensitive">参数是否敏感，如果敏感，不记入Log系统</param>
        /// <param name="isSharding">是否是Sharding字段</param>
        public void AddInParameter(string name, DbType dbType, object value, bool sensitive, bool isSharding)
        {
            base.Add(new StatementParameter
            {
                Name = name,
                DbType = dbType,
                Value = value,
                Direction = ParameterDirection.Input,
                IsSensitive = sensitive,
                IsShardingColumn = isSharding
            });
        }

        public void AddOutParameter(string name, DbType dbType, bool sensi = false)
        {
            base.Add(new StatementParameter
            {
                Name = name,
                DbType = dbType,
                Direction = ParameterDirection.Output,
                IsSensitive = sensi
            });
        }

        public void AddOutParameter(string name, DbType dbType, int size, bool sensi = false)
        {
            base.Add(new StatementParameter
            {
                Name = name,
                DbType = dbType,
                Direction = ParameterDirection.Output,
                Size = size,
                IsSensitive = sensi
            });
        }

        public void AddParameter(string name, DbType dbType, object value, int size, ParameterDirection dir, bool sensi = false)
        {
            base.Add(new StatementParameter
            {
                Name = name,
                DbType = dbType,
                Value = value,
                Direction = dir,
                Size = size,
                IsSensitive = sensi
            });
        }

        protected override string GetKeyForItem(StatementParameter item)
        {
            return item.Name;
        }
    }
}
